<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_DisplayAction extends f_core_Action
{
	public function execute()
	{		
		$pageId = $this->getRequestParameter('pageId');
		$pageLang = $this->getRequestParameter('pageLang');
        $isPreview = $this->getRequestParameter('preview');
        $previewMode = false;

        if(!is_null($isPreview) && $isPreview == '1' && $this->getUserContext()->hasRight('backoffice_accessBo'))
        {
            $previewMode = true;
        }

		$user = $this->getUser();

        $tempDocService = m_backoffice_services_TempDocService::getInstance();

        $page = null;

        // Page is getted by rewrited url or by direct module/action request ?
        if(is_null($pageId) && is_null($pageLang))
		{
            $page = m_website_services_URLService::getPageFromUrl(URL_ABSOLUTE_WO_HTTP.'/'.$this->getRequestParameter('url_rewrited'));
        }
        else
        {
            // Page exists ?
            if(f_document_Provider::getInstance()->checkDocument($pageId, $pageLang))
            {
                // Get page
                $page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $pageId, $pageLang);
            }
        }

        // Page is not published
        if(!is_null($page) && is_object($page) && $page instanceof m_website_documents_page && $page->getPublicationStatus() != 'PUBLISHED' && !$previewMode)
        {
            $page = null;
        }

        // Get temporary page in preview...
        if($previewMode && !is_null($page))
        {
            if($tempDocService->hasTemporary($page->getUniqueId(), $page->getLang()))
            {
                $page = $tempDocService->getTemporaryDocument($page->getUniqueId(), $page->getLang());
                $blocks = $page->getAllBlocks();

                foreach($blocks as $block)
                {
                    $block['preview'] = 'true';
                    $page->setBlockParamsById($block['blockId'], $block);
                }

                $this->getContext()->setContext('currentWebsite', $page->getWebsite());
            }
        }

        // No page... redirect to 404 page...
        if(is_null($page))
        {
            f_logs_Logger::getInstance()->errorLog('404 error : '.$this->getRequestParameter('url_rewrited'));
            m_website_services_PageService::getInstance()->redirectToTargetedPage('404', $this->getContext('currentWebsite')->getUniqueId(), $this->getContextLang());
        }

        if($page->hasTarget('404'))
        {
            header('HTTP/1.0 404 Not Found');
            header('Status: 404 Not Found');
        }

        if(!$page->isAuthorized($user) && !$previewMode)
        {
            $url = URL_RELATIVE.'/';
            
            if(!is_null($page->getPage401()))
            {
                if(f_document_Provider::getInstance()->checkDocument($page->getPage401(), $page->getLang()))
                {
                    $page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $page->getPage401(), $page->getLang());
                    $url = $page->getCompleteUrl();
                }
            }

            return f_core_Controller::getInstance()->redirect($url);
        }

        // Display the page

        $this->setDisplayLang($this->getContext()->getContext("currentWebsite")->getDisplayLang());
        $this->beginI18nWork($page->getLang());
        $context = $this->getContext()->setContext("currentPage", $page);

        $this->addToModel("page", $page);
        $this->addToModel("website", $this->getContext()->getContext("currentWebsite"));
        $this->setView('success');

        $this->endI18nWork();
	}
}